#include "../atomic_include/RadarData.h"


// å®ç°è·ååä¾å®ä¾çéææ¹æ³
RadarList& RadarList::getInstance() {
    static RadarList instance;
    return instance;
}

// æ·»å æ°çé·è¾¾
void RadarList::addRadar(int id) {
    for (const auto& radar : radars) {
        if (radar.radar_id == id) {
            return; // é·è¾¾å·²å­å¨ï¼æ ééå¤æ·»å 
        }
    }
    radars.emplace_back(id);
}
 
// å é¤æå®çé·è¾¾
void RadarList::deleteRadar(int id){
    for (auto it = radars.begin(); it != radars.end(); ++it) {
        if (it->radar_id == id) {
            radars.erase(it);
            return; // æåå é¤é·è¾¾
        }
    }
    // å¦ææªæ¾å°æå®çé·è¾¾ï¼æå°éè¯¯ä¿¡æ¯
    std::cerr << "Radar with ID " << id << " not found." << std::endl;
}


// è®¾ç½®æå®é·è¾¾çè·ç¦»
void RadarList::setDistance(int radar_id, double distance) {
    for (auto& radar : radars) {
        if (radar.radar_id == radar_id) {
            radar.distance = distance;
            return;
        }
    }
}

// è·åæå®é·è¾¾çè·ç¦»
double RadarList::getDistance(int radar_id) {
    for (const auto& radar : radars) {
        if (radar.radar_id == radar_id) {
            return radar.distance;
        }
    }
    return 0.0; // æªæ¾å°é·è¾¾ï¼è¿åé»è®¤å¼
}

// è®¾ç½®æå®é·è¾¾çè§åº¦
void RadarList::setAngle(int radar_id, double angle) {
    for (auto& radar : radars) {
        if (radar.radar_id == radar_id) {
            radar.angle = angle;
            return;
        }
    }
}

// è·åæå®é·è¾¾çè§åº¦
double RadarList::getAngle(int radar_id) {
    for (const auto& radar : radars) {
        if (radar.radar_id == radar_id) {
            return radar.angle;
        }
    }
    return 0.0; // æªæ¾å°é·è¾¾ï¼è¿åé»è®¤å¼
}

// è®¾ç½®æå®é·è¾¾çéåº¦
void RadarList::setVelocity(int radar_id, double velocity) {
    for (auto& radar : radars) {
        if (radar.radar_id == radar_id) {
            radar.velocity = velocity;
            return;
        }
    }
}

// è·åæå®é·è¾¾çéåº¦
double RadarList::getVelocity(int radar_id) {
    for (const auto& radar : radars) {
        if (radar.radar_id == radar_id) {
            return radar.velocity;
        }
    }
    return 0.0; // æªæ¾å°é·è¾¾ï¼è¿åé»è®¤å¼
}

// è®¾ç½®æå®é·è¾¾çç®æ ç±»å
void RadarList::setTargetType(int radar_id, const std::string& target_type) {
    for (auto& radar : radars) {
        if (radar.radar_id == radar_id) {
            radar.target_type = target_type;
            return;
        }
    }
}

// è·åæå®é·è¾¾çç®æ ç±»å
std::string RadarList::getTargetType(int radar_id) {
    for (const auto& radar : radars) {
        if (radar.radar_id == radar_id) {
            return radar.target_type;
        }
    }
    return ""; // æªæ¾å°é·è¾¾ï¼è¿åç©ºå­ç¬¦ä¸²
}

// è·åæå®ç´¢å¼çé·è¾¾ ID
int RadarList::getRadarID(int index) {
    if (index >= 0 && index < static_cast<int>(radars.size())) {
        return radars[index].radar_id;
    }
    return -1; // ç´¢å¼è¶çï¼è¿å -1
}


//å¯å¨é·è¾¾
int StartRadar(RequestSignal *requestSignal, OperationResult *operationResult){
    // éæºçæä¸ä¸ªæ£æµç»æ
    RadarList& radarList = RadarList::getInstance();
    int radar_id = requestSignal -> radar_id;
    radarList.addRadar(radar_id); // è¥é·è¾¾ä¸å­å¨åæ·»å 
    operationResult->status_code = 1;
    operationResult->description = "Radar is ready";
    return ATOMIC_SERVICE_SUCCESS;
}

//åæ­¢é·è¾¾
int StopRadar(RequestSignal *requestSignal, OperationResult *operationResult){
    // éæºçæä¸ä¸ªæ£æµç»æ
    RadarList& radarList = RadarList::getInstance();
    int radar_id = requestSignal -> radar_id;
    radarList.deleteRadar(radar_id); // è¥é·è¾¾ä¸å­å¨åæ·»å 
    operationResult->status_code = 1;
    operationResult->description = "Radar is stop";
    return ATOMIC_SERVICE_SUCCESS;
}

//è·åé·è¾¾ç¶æ
int statusDetect(RequestSignal *requestSignal, RadarResult *radarResult){
    // éæºçæä¸ä¸ªç¶æ
    RadarList& radarList = RadarList::getInstance();
    int radar_id = requestSignal -> radar_id;
    // åéæºæ°ä¸ºé·è¾¾çææ°æ®
    radarList.setAngle(radar_id, 100.0);
    radarList.setDistance(radar_id, 100.0);
    radarList.setVelocity(radar_id, 100.0);
    radarList.setTargetType(radar_id, "person");

    //åä¸ºradarStatusè¿åæ°æ®
    radarResult -> distance = radarList.getDistance(radar_id);
    radarResult -> angle = radarList.getAngle(radar_id);
    radarResult -> velocity = radarList.getVelocity(radar_id);
    radarResult -> target_type = radarList.getTargetType(radar_id);
    return ATOMIC_SERVICE_SUCCESS;
}


 void StartRadar_func(RequestSignal *request, OperationResult *reply){
 	// Function implementation TODO
     int ret_type = StartRadar(request, reply);
     reply->return_type = ret_type;
 }

 void StopRadar_func(RequestSignal *request, OperationResult *reply){
 	// Function implementation TODO
     int ret_type = StopRadar(request, reply);
     reply->return_type = ret_type;
 }

 void statusDetect_func(RequestSignal *request, RadarResult *reply){
 	// Function implementation TODO
     int ret_type = statusDetect(request, reply);
     reply->return_type = ret_type;
 }

